package com.parttimetask.activities;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.parttimetask.App;
import com.parttimetask.databinding.ActivityFeedbackBinding;
import com.parttimetask.utils.Http;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class FeedbackActivity extends AppCompatActivity {
    private ActivityFeedbackBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityFeedbackBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        setTitle("Feedback");
        binding.feedbackSubmit.setOnClickListener(view -> {
            if (!binding.feedbackEt.getText().toString().equals("")){
                try {
                    JSONObject jo=new JSONObject();
                    jo.put("content",binding.feedbackEt.getText().toString());
                    jo.put("mobile", App.sp.getString("mobile",""));
                    Http.post(jo, "feedback/add/", new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            e.printStackTrace();
                        }
                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) {
                            runOnUiThread(() -> {
                                Toast.makeText(FeedbackActivity.this, "Thank for your feedback", Toast.LENGTH_SHORT).show();
                                finish();
                            });
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
}
